package edu.nf.exam.dao.impl;

import edu.nf.exam.dao.UserInfoDao;
import edu.nf.exam.entity.UserInfo;
import edu.nf.exam.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author : PYY
 * @create 2021/6/17
 */
public class UserInfoDaoImpl implements UserInfoDao {
    /**
     * 登录
     * @param userName
     * @return
     */
    @Override
    public UserInfo getUserByName(String userName) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        UserInfo user = sqlSession.getMapper(UserInfoDao.class).getUserByName(userName);
        sqlSession.close();
        return user;
    }

    /**
     * 多条件查询用户
     * @param userInfo
     * @return
     */
    @Override
    public List<UserInfo> select(UserInfo userInfo) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        List<UserInfo> list = sqlSession.getMapper(UserInfoDao.class).select(userInfo);
        sqlSession.close();
        return list;
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public int register(UserInfo user) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        UserInfoDao mapper = sqlSession.getMapper(UserInfoDao.class);
        int r = mapper.register(user);
        sqlSession.close();
        return r;
    }

    /**
     * 修改用户信息
     * @param userInfo
     * @return
     */
    @Override
    public int update(UserInfo userInfo) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        int r = sqlSession.getMapper(UserInfoDao.class).update(userInfo);
        sqlSession.close();
        return r;
    }

    /**
     * 分页查询
     * @param userId 用户id
     * @param userName 条件查询，根据角色
     * @param role 根据身份查询
     * @param pageNum 当前页 @Param注解的value值必须是pageNum,
     * @param pageSize 每页记录数 @Param注解的value值必须是pageSize
     * @return
     */
    @Override
    public List<UserInfo> listUserInfo(Integer userId, String userName, Integer role, Integer pageNum, Integer pageSize) {
        SqlSession sqlSession =MyBatisUtil.getSqlSession(true);
        List<UserInfo> list = sqlSession.getMapper(UserInfoDao.class).listUserInfo(userId,userName,role, pageNum, pageSize);
        sqlSession.close();
        return list;
    }
}
